package com.itheima.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CustomException;
import com.itheima.common.TokenHolder;
import com.itheima.dao.DishFlavorMapper;
import com.itheima.dao.DishMapper;
import com.itheima.dto.SaveDishDto;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.DishService;
import com.itheima.vo.DishDownVo;
import com.itheima.vo.DishListVo;
import com.itheima.vo.DishPageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishPageVo> getPage(Integer page, Integer pageSize, String name) {

        int skip = (page - 1) * pageSize;

        List<DishPageVo> dishPageVoList = dishMapper.getPage(skip, pageSize, name);

        Page<DishPageVo> dishPage = new Page<>();

        Long aLong = dishMapper.selectTotalByName();

        dishPage.setTotal(aLong);
        //dishPage.setTotal(dishPageVoList.size());
        dishPage.setRecords(dishPageVoList);
        dishPage.setSize(pageSize);
        dishPage.setCurrent(page);
        return dishPage;
    }

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Boolean save(SaveDishDto dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);

        dish.setId(new Snowflake().nextIdStr());
        dish.setSort("5");
        dish.setCreateUser(TokenHolder.getCurrentId());
        dish.setUpdateUser(TokenHolder.getCurrentId());
        dish.setUpdateTime(LocalDateTime.now().toString());
        dish.setCreateTime(LocalDateTime.now().toString());
        dish.setIsDeleted("0");

        List<DishFlavor> flavors = dto.getFlavors();
        if (CollUtil.isNotEmpty(flavors)) {
            for (DishFlavor flavor : flavors) {
                flavor.setId(new Snowflake().nextIdStr());
                flavor.setDishId(dish.getId());
                flavor.setCreateUser(TokenHolder.getCurrentId());
                flavor.setUpdateUser(TokenHolder.getCurrentId());
                flavor.setUpdateTime(LocalDateTime.now().toString());
                flavor.setCreateTime(LocalDateTime.now().toString());
                flavor.setIsDeleted("0");
            }
        }
        int insert = dishMapper.insert(dish);
        dishFlavorMapper.insert(flavors);

        return insert > 0;
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public Dish getById(String id) {

        Dish dish = dishMapper.selectById(id);

        List<DishFlavor> dishFlavors = dishFlavorMapper.selectById(dish.getId());


        dish.setFlavors(dishFlavors);

        return dish;
    }

    /**
     * 修改
     *
     * @param dish
     * @return
     */
    @Override
    public Boolean updateDish(Dish dish) {
        dish.setUpdateTime(LocalDateTime.now().toString());
        dish.setUpdateUser(TokenHolder.getCurrentId());
        int update = dishMapper.update(dish);
        //删除菜品的口味
        int deleted = dishFlavorMapper.deleted(dish.getId());
        List<DishFlavor> flavors = dish.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setUpdateTime(LocalDateTime.now().toString());
            flavor.setUpdateUser(TokenHolder.getCurrentId());

        }
        dishFlavorMapper.insert(flavors);
        return update > 0;
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean deleteDish(String[] ids) {

        int i = dishMapper.updateIsDeletedById(ids);
        int i1 = dishFlavorMapper.updateIsDeletedById(ids);

        return i > ids.length && i1 > ids.length;
    }

    /**
     * 根据id启停售
     *
     * @param ids
     * @param status
     * @return
     */
    @Override
    public Boolean updateStatus(String ids, Integer status) {
        String[] split = ids.split(",");
        int i = dishMapper.updateStatusById(split, status);

        return i > 0;
    }

    /**
     * 下拉列表
     *
     * @param categoryId id
     * @return 集合
     */
    @Override
    public List<DishDownVo> dishDownList(String categoryId,String name) {

        List<DishDownVo> dishDownList = dishMapper.selectByCategoryId(categoryId,name);
        if (CollUtil.isNotEmpty(dishDownList)) {
            for (DishPageVo dish : dishDownList) {
                List<DishFlavor> flavorList = dishFlavorMapper.selectByDishId(dish.getId());
                dish.setFlavors(flavorList);
            }
            return dishDownList;
        }
        return dishDownList;
    }

    /**
     * 菜品列表
     *
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<Dish> getList(String categoryId, Integer status) {

        List<Dish> dishList = dishMapper.getList(categoryId, status);
        if(CollUtil.isEmpty(dishList)) {
            throw new CustomException("没有数据");
        }
        for (Dish dishListVo : dishList) {
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectById(dishListVo.getId());

            dishListVo.setFlavors(dishFlavors);
        }
        return dishList;
    }


}
